home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / symm-tdep.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  12KB  |  495 lines

  1. /* Sequent Symmetry target interface, for GDB when running under Unix.
  2.    Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* many 387-specific items of use taken from i386-dep.c */
  21.  
  22. #include <stdio.h>
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "frame.h"
  26. #include "inferior.h"
  27. #include "symtab.h"
  28.  
  29. #include <signal.h>
  30. #include <sys/param.h>
  31. #include <sys/user.h>
  32. #include <sys/dir.h>
  33. #include <sys/ioctl.h>
  34. #include <sys/stat.h>
  35. #include "gdbcore.h"
  36. #include <fcntl.h>
  37.  
  38. static long i386_get_frame_setup ();
  39. static i386_follow_jump ();
  40.  
  41. #include <sgtty.h>
  42. #define TERMINAL struct sgttyb
  43.  
  44. exec_file_command (filename, from_tty)
  45.      char *filename;
  46.      int from_tty;
  47. {
  48.   int val;
  49.  
  50.   /* Eliminate all traces of old exec file.
  51.      Mark text segment as empty.  */
  52.  
  53.   if (execfile)
  54.     free (execfile);
  55.   execfile = 0;
  56.   data_start = 0;
  57.   data_end -= exec_data_start;
  58.   text_start = 0;
  59.   text_end = 0;
  60.   exec_data_start = 0;
  61.   exec_data_end = 0;
  62.   if (execchan >= 0)
  63.     close (execchan);
  64.   execchan = -1;
  65.  
  66.   /* Now open and digest the file the user requested, if any.  */
  67.  
  68.   if (filename)
  69.     {
  70.       filename = tilde_expand (filename);
  71.       make_cleanup (free, filename);
  72.       
  73.       execchan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  74.             &execfile);
  75.       if (execchan < 0)
  76.     perror_with_name (filename);
  77.  
  78. #ifdef COFF_FORMAT
  79.       {
  80.     int aout_hdrsize;
  81.     int num_sections;
  82.  
  83.     if (read_file_hdr (execchan, &file_hdr) < 0)
  84.       error ("\"%s\": not in executable format.", execfile);
  85.  
  86.     aout_hdrsize = file_hdr.f_opthdr;
  87.     num_sections = file_hdr.f_nscns;
  88.  
  89.     if (read_aout_hdr (execchan, &exec_aouthdr, aout_hdrsize) < 0)
  90.       error ("\"%s\": can't read optional aouthdr", execfile);
  91.  
  92.     if (read_section_hdr (execchan, _TEXT, &text_hdr, num_sections,
  93.                   aout_hdrsize) < 0)
  94.       error ("\"%s\": can't read text section header", execfile);
  95.  
  96.     if (read_section_hdr (execchan, _DATA, &data_hdr, num_sections,
  97.                   aout_hdrsize) < 0)
  98.       error ("\"%s\": can't read data section header", execfile);
  99.  
  100.     text_start = exec_aouthdr.text_start;
  101.     text_end = text_start + exec_aouthdr.tsize;
  102.     text_offset = text_hdr.s_scnptr;
  103.     exec_data_start = exec_aouthdr.data_start;
  104.     exec_data_end = exec_data_start + exec_aouthdr.dsize;
  105.     exec_data_offset = data_hdr.s_scnptr;
  106.     data_start = exec_data_start;
  107.     data_end += exec_data_start;
  108.     exec_mtime = file_hdr.f_timdat;
  109.       }
  110. #else /* not COFF_FORMAT */
  111.       {
  112.     struct stat st_exec;
  113.  
  114.     val = myread (execchan, &exec_aouthdr, sizeof (AOUTHDR));
  115.  
  116.     if (val < 0)
  117.       perror_with_name (filename);
  118.  
  119.     text_start = N_ADDRADJ(exec_aouthdr);
  120.         exec_data_start = round(exec_aouthdr.a_text, NBPG*CLSIZE);
  121.     text_offset = N_TXTOFF (exec_aouthdr);
  122.     exec_data_offset = N_TXTOFF (exec_aouthdr) + exec_aouthdr.a_text;
  123.     text_end = exec_aouthdr.a_text;
  124.         exec_data_end = exec_data_start + exec_aouthdr.a_data;
  125.     data_start = exec_data_start;
  126.     data_end = data_start + exec_aouthdr.a_data;
  127.     exec_data_offset = N_TXTOFF(exec_aouthdr);
  128.     fstat (execchan, &st_exec);
  129.     exec_mtime = st_exec.st_mtime;
  130.       }
  131. #endif /* not COFF_FORMAT */
  132.  
  133.       validate_files ();
  134.     }
  135.   else if (from_tty)
  136.     printf ("No exec file now.\n");
  137.  
  138.   /* Tell display code (if any) about the changed file name.  */
  139.   if (exec_file_display_hook)
  140.     (*exec_file_display_hook) (filename);
  141. }
  142.  
  143. /* rounds 'one' up to divide evenly by 'two' */
  144.  
  145. int
  146. round(one,two)
  147. register int one, two;
  148.  
  149. {
  150.     register int temp;
  151.     temp = (one/two)*two;
  152.     if (one != temp) {
  153.     temp += two;
  154.     }
  155.     return temp;
  156. }
  157.  
  158.  
  159. static CORE_ADDR codestream_next_addr;
  160. static CORE_ADDR codestream_addr;
  161. static unsigned char codestream_buf[sizeof (int)];
  162. static int codestream_off;
  163. static int codestream_cnt;
  164.  
  165. #define codestream_tell() (codestream_addr + codestream_off)
  166. #define codestream_peek() (codestream_cnt == 0 ? \
  167.                codestream_fill(1): codestream_buf[codestream_off])
  168. #define codestream_get() (codestream_cnt-- == 0 ? \
  169.              codestream_fill(0) : codestream_buf[codestream_off++])
  170.  
  171.  
  172. static unsigned char 
  173. codestream_fill (peek_flag)
  174. {
  175.   codestream_addr = codestream_next_addr;
  176.   codestream_next_addr += sizeof (int);
  177.   codestream_off = 0;
  178.   codestream_cnt = sizeof (int);
  179.   read_memory (codestream_addr,
  180.            (unsigned char *)codestream_buf,
  181.            sizeof (int));
  182.   
  183.   if (peek_flag)
  184.     return (codestream_peek());
  185.   else
  186.     return (codestream_get());
  187. }
  188.  
  189. static void
  190. codestream_seek (place)
  191. {
  192.   codestream_next_addr = place & -sizeof (int);
  193.   codestream_cnt = 0;
  194.   codestream_fill (1);
  195.   while (codestream_tell() != place)
  196.     codestream_get ();
  197. }
  198.  
  199. static void
  200. codestream_read (buf, count)
  201.      unsigned char *buf;
  202. {
  203.   unsigned char *p;
  204.   int i;
  205.   p = buf;
  206.   for (i = 0; i < count; i++)
  207.     *p++ = codestream_get ();
  208. }
  209.  
  210. /*
  211.  * Following macro translates i386 opcode register numbers to Symmetry
  212.  * register numbers.  This is used by FRAME_FIND_SAVED_REGS.
  213.  *
  214.  *           %eax  %ecx  %edx  %ebx  %esp  %ebp  %esi  %edi
  215.  * i386        0     1     2     3     4     5     6     7
  216.  * Symmetry    0     2     1     5    14    15     6     7
  217.  *
  218.  */
  219. #define I386_REGNO_TO_SYMMETRY(n) \
  220. ((n)==0?0 :(n)==1?2 :(n)==2?1 :(n)==3?5 :(n)==4?14 :(n)==5?15 :(n))
  221.  
  222. /* from i386-dep.c */
  223. i386_frame_find_saved_regs (fip, fsrp)
  224.      struct frame_info *fip;
  225.      struct frame_saved_regs *fsrp;
  226. {
  227.   unsigned long locals;
  228.   unsigned char *p;
  229.   unsigned char op;
  230.   CORE_ADDR dummy_bottom;
  231.   CORE_ADDR adr;
  232.   int i;
  233.   
  234.   bzero (fsrp, sizeof *fsrp);
  235.   
  236.   /* if frame is the end of a dummy, compute where the
  237.    * beginning would be
  238.    */
  239.   dummy_bottom = fip->frame - 4 - NUM_REGS*4 - CALL_DUMMY_LENGTH;
  240.   
  241.   /* check if the PC is in the stack, in a dummy frame */
  242.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  243.     {
  244.       /* all regs were saved by push_call_dummy () */
  245.       adr = fip->frame - 4;
  246.       for (i = 0; i < NUM_REGS; i++) 
  247.     {
  248.       fsrp->regs[i] = adr;
  249.       adr -= 4;
  250.     }
  251.       return;
  252.     }
  253.   
  254.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  255.   
  256.   if (locals >= 0) 
  257.     {
  258.       adr = fip->frame - 4 - locals;
  259.       for (i = 0; i < 8; i++) 
  260.     {
  261.       op = codestream_get ();
  262.       if (op < 0x50 || op > 0x57)
  263.         break;
  264.       fsrp->regs[I386_REGNO_TO_SYMMETRY(op - 0x50)] = adr;
  265.       adr -= 4;
  266.     }
  267.     }
  268.   
  269.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  270.   fsrp->regs[FP_REGNUM] = fip->frame;
  271. }
  272.  
  273. static long
  274. i386_get_frame_setup (pc)
  275. {
  276.   unsigned char op;
  277.   
  278.   codestream_seek (pc);
  279.   
  280.   i386_follow_jump ();
  281.   
  282.   op = codestream_get ();
  283.   
  284.   if (op == 0x58) /* popl %eax */
  285.     {
  286.       /*
  287.        * this function must start with
  288.        * 
  289.        *    popl %eax          0x58
  290.        *    xchgl %eax, (%esp)  0x87 0x04 0x24
  291.        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
  292.        *
  293.        * (the system 5 compiler puts out the second xchg
  294.        * inst, and the assembler doesn't try to optimize it,
  295.        * so the 'sib' form gets generated)
  296.        * 
  297.        * this sequence is used to get the address of the return
  298.        * buffer for a function that returns a structure
  299.        */
  300.       int pos;
  301.       unsigned char buf[4];
  302.       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
  303.       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
  304.       pos = codestream_tell ();
  305.       codestream_read (buf, 4);
  306.       if (bcmp (buf, proto1, 3) == 0)
  307.     pos += 3;
  308.       else if (bcmp (buf, proto2, 4) == 0)
  309.     pos += 4;
  310.       
  311.       codestream_seek (pos);
  312.       op = codestream_get (); /* update next opcode */
  313.     }
  314.   
  315.   if (op == 0x55)             /* pushl %esp */
  316.     {
  317.       if (codestream_get () != 0x8b)    /* movl %esp, %ebp (2bytes) */
  318.     return (-1);
  319.       if (codestream_get () != 0xec)
  320.     return (-1);
  321.       /*
  322.        * check for stack adjustment 
  323.        *
  324.        *  subl $XXX, %esp
  325.        *
  326.        * note: you can't subtract a 16 bit immediate
  327.        * from a 32 bit reg, so we don't have to worry
  328.        * about a data16 prefix 
  329.        */
  330.       op = codestream_peek ();
  331.       if (op == 0x83)  /* subl with 8 bit immed */
  332.     {
  333.       codestream_get ();
  334.       if (codestream_get () != 0xec)
  335.         return (-1);
  336.       /* subl with signed byte immediate 
  337.        * (though it wouldn't make sense to be negative)
  338.        */
  339.       return (codestream_get());
  340.     }
  341.       else if (op == 0x81)  /* subl with 32 bit immed */
  342.     {
  343.       int locals;
  344.       if (codestream_get () != 0xec)
  345.         return (-1);
  346.       /* subl with 32 bit immediate */
  347.       codestream_read ((unsigned char *)&locals, 4);
  348.       return (locals);
  349.     } 
  350.       else 
  351.     {
  352.       return (0);
  353.     }
  354.     } 
  355.   else if (op == 0xc8) 
  356.     {
  357.       /* enter instruction: arg is 16 unsigned immed */
  358.       unsigned short slocals;
  359.       codestream_read ((unsigned char *)&slocals, 2);
  360.       codestream_get (); /* flush final byte of enter instruction */
  361.       return (slocals);
  362.     }
  363.   return (-1);
  364. }
  365.  
  366. /* next instruction is a jump, move to target */
  367. static
  368. i386_follow_jump ()
  369. {
  370.   int long_delta;
  371.   short short_delta;
  372.   char byte_delta;
  373.   int data16;
  374.   int pos;
  375.   
  376.   pos = codestream_tell ();
  377.   
  378.   data16 = 0;
  379.   if (codestream_peek () == 0x66)
  380.     {
  381.       codestream_get ();
  382.       data16 = 1;
  383.     }
  384.   
  385.   switch (codestream_get ())
  386.     {
  387.     case 0xe9:
  388.       /* relative jump: if data16 == 0, disp32, else disp16 */
  389.       if (data16)
  390.     {
  391.       codestream_read ((unsigned char *)&short_delta, 2);
  392.       pos += short_delta + 3; /* include size of jmp inst */
  393.     }
  394.       else
  395.     {
  396.       codestream_read ((unsigned char *)&long_delta, 4);
  397.       pos += long_delta + 5;
  398.     }
  399.       break;
  400.     case 0xeb:
  401.       /* relative jump, disp8 (ignore data16) */
  402.       codestream_read ((unsigned char *)&byte_delta, 1);
  403.       pos += byte_delta + 2;
  404.       break;
  405.     }
  406.   codestream_seek (pos + data16);
  407. }
  408.  
  409. /* return pc of first real instruction */
  410. /* from i386-dep.c */
  411.  
  412. i386_skip_prologue (pc)
  413. {
  414.   unsigned char op;
  415.   int i;
  416.   
  417.   if (i386_get_frame_setup (pc) < 0)
  418.     return (pc);
  419.   
  420.   /* found valid frame setup - codestream now points to 
  421.    * start of push instructions for saving registers
  422.    */
  423.   
  424.   /* skip over register saves */
  425.   for (i = 0; i < 8; i++)
  426.     {
  427.       op = codestream_peek ();
  428.       /* break if not pushl inst */
  429.       if (op < 0x50 || op > 0x57) 
  430.     break;
  431.       codestream_get ();
  432.     }
  433.   
  434.   i386_follow_jump ();
  435.   
  436.   return (codestream_tell ());
  437. }
  438.  
  439. symmetry_extract_return_value(type, regbuf, valbuf)
  440.      struct type *type;
  441.      char *regbuf;
  442.      char *valbuf;
  443. {
  444.   union { 
  445.     double    d; 
  446.     int    l[2]; 
  447.   } xd; 
  448.   int i;
  449.   float f;
  450.  
  451.   if (TYPE_CODE_FLT == TYPE_CODE(type)) { 
  452.     for (i = 0; i < misc_function_count; i++) {
  453.       if (!strcmp(misc_function_vector[i].name, "1167_flt"))
  454.     break;
  455.     }
  456.     if (i < misc_function_count) {
  457.       /* found "1167_flt" means 1167, %fp2-%fp3 */ 
  458.       /* float & double; 19= %fp2, 20= %fp3 */
  459.       /* no single precision on 1167 */
  460.       xd.l[1] = *((int *)®buf[REGISTER_BYTE(19)]);
  461.       xd.l[0] = *((int *)®buf[REGISTER_BYTE(20)]);
  462.       switch (TYPE_LENGTH(type)) {
  463.       case 4:
  464.     f = (float) xd.d;
  465.     bcopy(&f, valbuf, TYPE_LENGTH(type));
  466.     break;
  467.       case 8:
  468.     bcopy(&xd.d, valbuf, TYPE_LENGTH(type)); 
  469.     break;
  470.       default:
  471.     error("Unknown floating point size");
  472.     break;
  473.       }
  474.     } else { 
  475.       /* 387 %st(0), gcc uses this */ 
  476.       i387_to_double(((int *)®buf[REGISTER_BYTE(3)]),
  477.              &xd.d); 
  478.       switch (TYPE_LENGTH(type)) {
  479.       case 4:            /* float */
  480.     f = (float) xd.d;
  481.     bcopy(&f, valbuf, 4); 
  482.     break;
  483.       case 8:            /* double */
  484.     bcopy(&xd.d, valbuf, 8);
  485.     break;
  486.       default:
  487.     error("Unknown floating point size");
  488.     break;
  489.       }
  490.     }
  491.   } else { 
  492.     bcopy (regbuf, valbuf, TYPE_LENGTH (type)); 
  493.   }
  494. }
  495.